﻿using System.Collections.Generic;
using System.Linq;
using Loki.Bot;
using Loki.Game.GameData;
using Loki.Game.Objects;

namespace toNyxPhysRanger
{
    public class Target
    {
        /// <summary>
        ///     Returns the best focusable object on the TargetList. (optimized for Facebreaker builds ! be careful !)
        /// </summary>
        public static Monster BestTarget
        {
            get
            {
                // Special totem focus
                if (SpecialTotemsCount > 0)
                    return
                        TargetList
                            .Where(tar => SpecialTotemsFocus.Contains(tar.Name))
                            .OrderBy(tar => tar.Distance)
                            .First();

                // Unique mobs focus
                if (SpecialMobCount(Rarity.Unique) > 0)
                    return
                        TargetList
                            .Where(tar => tar.Rarity == Rarity.Unique)
                            .OrderBy(tar => tar.Distance)
                            .First();

                // Rare mobs focus
                if (SpecialMobCount(Rarity.Rare) > 0)
                    return
                        TargetList
                            .Where(tar => tar.Rarity == Rarity.Rare)
                            .OrderBy(tar => tar.Distance)
                            .First();

                // Must Focus Targets
                if (MustFocusUnitsCount > 0)
                    return
                        TargetList
                            .Where(tar => MustFocusUnits.Contains(tar.Name))
                            .OrderBy(tar => tar.Distance)
                            .First();

                // Normal target (by distance? checking is it works better)
                return (Monster)Targeting.Combat.Targets.OrderBy(tar => tar.Distance).FirstOrDefault();
            }
        }

        public static IEnumerable<Monster> TargetList
        {
            get { return Targeting.Combat.Targets.OfType<Monster>(); }
        }

        public static int SpecialMobCount(Rarity myRarity)
        {
            return TargetList.Count(tar => tar.Rarity == myRarity);
        }

        public static int SpecialTotemsCount
        {
            get { return TargetList.Count(tar => SpecialTotemsFocus.Contains(tar.Name)); }
        }

        public static int MustFocusUnitsCount
        {
            get { return TargetList.Count(tar => MustFocusUnits.Contains(tar.Name)); }
        }

        private static readonly List<string> MustFocusUnits = new List<string>
        {
               "Ancient Construct", // Poisonous shit in Ancient Pyramid
        };

        // List of the totems we need to focus, that can be pain in the ass and could slow down bot
        private static readonly List<string> SpecialTotemsFocus = new List<string>
        {
            "Detonating Totem"                                                         ,
            "Necromantic Totem",
            "Vaal Totem"
        };

    }
}
